﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml;

using SSOClient.ClientDAL;
using SSOClient.IClientDAL;

namespace YNLibWin
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            login();
        }

        private void login()
        {
            if (txtUsername.Text.Trim() == "")
            {
                MessageBox.Show("请输入用户名!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (txtPassword.Text.Trim() == "")
            {
                MessageBox.Show("请输入密码!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            SSO.WebServ_LoginOption ws = new SSO.WebServ_LoginOption();
            SSO.LoginInfo li = ws.UserLogin(txtUsername.Text.Trim(), txtPassword.Text.Trim());
            
            if (li.LoginState == "1")
            {
                string strSSOApplicationID = System.Configuration.ConfigurationManager.AppSettings["SSOApplicationID"].ToString();
                SSO.UsersInfo userInfo = ws.GetUserInfo(this.txtUsername.Text.Trim(), strSSOApplicationID);
                if (userInfo.UserRole == 1)
                {
                    //将本次登陆的用户名与项目名称存起来
                    WriteLoginUnitXML();
                    this.Visible = false;//本窗体隐藏
                    frmMain fMain = new frmMain();
                    fMain.ShowDialog();
                }
                else
                {
                    MessageBox.Show("对不起，你不是管理员！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("对不起，用户名或密码错误！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //if (lum.IsEnable(txtUsername.Text.Trim()) == false)
            //{

            //    if (lum.CheckUserPassword(txtUsername.Text.Trim(), txtPassword.Text))
            //    {
            //        SysParams.UserID = txtUsername.Text.Trim();
            //        SysParams.UserName = lum.GetLoginUserName(txtUsername.Text.Trim());

            //        //将本次登陆的用户名与项目名称存起来
            //        WriteLoginUnitXML();
            //        //this.DialogResult = DialogResult.OK;
            //        this.Visible = false;

            //        //写日志
            //        SysLog.AddOperateLog(SysParams.UserName, "系统登陆", "", SysParams.UserName + "用户进入系统");


            //        frmErpMain frmErpMain = new frmErpMain();
            //        frmErpMain.ShowDialog();

            //    }
            //    else
            //    {
            //        MessageBox.Show("你输入的用户账号或用户密码有错误,请重输!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            //        return;
            //    }
            //}
            //else
            //{
            //    MessageBox.Show("此用户已禁用，无法登陆系统!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            //    return;
            //}
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() == "\r")
            {
                login();
            }
        }

        /// <summary>
        /// 取出登陆用户名与项目名称
        /// </summary>
        private void GetLoginUnitXML()
        {
            string fileName = Application.StartupPath + @"\HistoryLogin.xml";
            XmlDocument myXmlDocument = new XmlDocument();
            myXmlDocument.Load(fileName);
            XmlNode rootNode = myXmlDocument.DocumentElement;
            //用户上次登陆账号
            string userid = rootNode.ChildNodes[0].ChildNodes[0].Attributes["value"].Value;

            this.txtUsername.Text = userid;
        }

        /// <summary>
        /// 将登陆用户与项目名称存入xml中
        /// </summary>
        private void WriteLoginUnitXML()
        {

            string fileName = Application.StartupPath + @"\HistoryLogin.xml";

            XmlDocument myXmlDocument = new XmlDocument();
            myXmlDocument.Load(fileName);
            XmlNode rootNode = myXmlDocument.DocumentElement;
            //用户上次登陆用户名
            rootNode.ChildNodes[0].ChildNodes[0].Attributes["value"].Value = this.txtUsername.Text;

            myXmlDocument.Save(Application.StartupPath + @"\HistoryLogin.xml");
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            //将上次登陆的用户与项目名称加载出来
            GetLoginUnitXML();
        }

    }
}
